package com.newenergy.wisehome.data.ormlite.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;
import com.newenergy.wisehome.data.bean.AirConditioning;
import com.newenergy.wisehome.data.bean.BLDeviceInfo;
import com.newenergy.wisehome.data.bean.BallLight;
import com.newenergy.wisehome.data.bean.BathHeater;
import com.newenergy.wisehome.data.bean.Clotheshorse;
import com.newenergy.wisehome.data.bean.Curtain;
import com.newenergy.wisehome.data.bean.DeviceCategory;
import com.newenergy.wisehome.data.bean.ElectricHeater;
import com.newenergy.wisehome.data.bean.Family;
import com.newenergy.wisehome.data.bean.FamilyUnique;
import com.newenergy.wisehome.data.bean.Fan;
import com.newenergy.wisehome.data.bean.FanLamp;
import com.newenergy.wisehome.data.bean.HealthBasin;
import com.newenergy.wisehome.data.bean.Icon;
import com.newenergy.wisehome.data.bean.SecneGroup;
import com.newenergy.wisehome.data.bean.SecneMode;
import com.newenergy.wisehome.data.bean.SplitSwitch;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;


public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
    private static final String DATABASE_NAME = "BleLight.db";

    private Map<String, Dao> daos = new HashMap<String, Dao>();

    private DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, 12);
    }

    @Override
    public void onCreate(SQLiteDatabase database,
                         ConnectionSource connectionSource) {
        try {
            TableUtils.createTableIfNotExists(connectionSource, BallLight.class);
            TableUtils.createTableIfNotExists(connectionSource, Family.class);
            TableUtils.createTableIfNotExists(connectionSource, Icon.class);
            TableUtils.createTableIfNotExists(connectionSource, SecneMode.class);
            TableUtils.createTableIfNotExists(connectionSource, SecneGroup.class);
            TableUtils.createTableIfNotExists(connectionSource, FamilyUnique.class);
            TableUtils.createTableIfNotExists(connectionSource, DeviceCategory.class);
            TableUtils.createTableIfNotExists(connectionSource, Fan.class);
            TableUtils.createTableIfNotExists(connectionSource, FanLamp.class);
            TableUtils.createTableIfNotExists(connectionSource, ElectricHeater.class);
            TableUtils.createTableIfNotExists(connectionSource, BathHeater.class);
            TableUtils.createTableIfNotExists(connectionSource, Curtain.class);
            TableUtils.createTableIfNotExists(connectionSource, Clotheshorse.class);
            TableUtils.createTableIfNotExists(connectionSource, AirConditioning.class);
            TableUtils.createTableIfNotExists(connectionSource, HealthBasin.class);
            TableUtils.createTableIfNotExists(connectionSource, BLDeviceInfo.class);
//            TableUtils.createTable(connectionSource, SplitSwitch.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) {
        try {
            if(oldVersion < 6){
                DatabaseUtil.upgradeTable(database,connectionSource,FamilyUnique.class,DatabaseUtil.OPERATION_TYPE.ADD);
                DatabaseUtil.upgradeTable(database,connectionSource,Family.class,DatabaseUtil.OPERATION_TYPE.ADD);
                DatabaseUtil.upgradeTable(database,connectionSource,BallLight.class,DatabaseUtil.OPERATION_TYPE.ADD);
            }

            if (oldVersion<7){
                TableUtils.dropTable(connectionSource, SplitSwitch.class, true);
            }


            if (oldVersion <8) {
                DatabaseUtil.upgradeTable(database,connectionSource,BallLight.class,DatabaseUtil.OPERATION_TYPE.DELETE);
            }

            if (oldVersion < 11) {
                DatabaseUtil.upgradeTable(database,connectionSource,DeviceCategory.class,DatabaseUtil.OPERATION_TYPE.ADD);
                DatabaseUtil.upgradeTable(database,connectionSource,Family.class,DatabaseUtil.OPERATION_TYPE.ADD);
            }
            if (oldVersion < 12) {
                DatabaseUtil.upgradeTable(database,connectionSource,DeviceCategory.class,DatabaseUtil.OPERATION_TYPE.ADD);
                DatabaseUtil.upgradeTable(database,connectionSource,Family.class,DatabaseUtil.OPERATION_TYPE.ADD);
            }
            onCreate(database,connectionSource);

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private static DatabaseHelper instance;

    /**
     * 单例获取该Helper
     *
     * @param context
     * @return
     */
    public static synchronized DatabaseHelper getHelper(Context  context) {
        if (context == null) {
            Log.e("LFX", "DatabaseHelper getHelper:context == null " );
            throw  new RuntimeException("DatabaseHelper context == null");
        }else {
 	     context = context.getApplicationContext();
        }
        if (instance == null) {
            synchronized (DatabaseHelper.class) {
                if (instance == null)
                    instance = new DatabaseHelper(context);
            }
        }

        return instance;
    }

    public synchronized Dao getDao(Class clazz) throws SQLException {
        Dao dao = null;
        String className = clazz.getSimpleName();

        if (daos.containsKey(className)) {
            dao = daos.get(className);
        }
        if (dao == null) {
            dao = super.getDao(clazz);
            daos.put(className, dao);
        }
        return dao;
    }

    /**
     * 释放资源
     */
    @Override
    public void close() {
        super.close();
        for (String key : daos.keySet()) {
            Dao dao = daos.get(key);
            dao = null;
        }
    }

}
